﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class RoomController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public RoomController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
         )
        {
            JwtSettings jwtSettings = new JwtSettings();
            jwtSettings.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwtSettings.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwtSettings.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwtSettings;
            //_jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }
        /// <summary>
        /// 新建房间
        /// </summary>
        [HttpPost]
        [Route("AddRoom")]
        public ResponseMessage AddRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomBUS BUS = new RoomBUS();
            try
            {
                roomInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.AddRoom(roomInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增房间";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增房间异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 修改房间
        /// </summary>
        [HttpPost]
        [Route("EditRoom")]
        public ResponseMessage EditRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomBUS BUS = new RoomBUS();
            try
            {
                message = BUS.EditRoom(roomInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "修改房间";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "修改房间信息异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 删除房间
        /// </summary>
        [HttpPost]
        [Route("DeleteRoom")]
        public ResponseMessage DeleteRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomBUS BUS = new RoomBUS();
            try
            {
                message = BUS.DeleteRoom(roomInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除房间";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除房间异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询房间
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryRoomByBuildID")]
        public ResponseMessage QueryRoomByBuildID(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomBUS BUS = new RoomBUS();
            try
            {
                message = BUS.QueryRoom(roomInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询房间";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询房间异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 载入房间信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("LoadRoom")]
        public ResponseMessage LoadRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomBUS BUS = new RoomBUS();
            try
            {
                message = BUS.LoadRoom(roomInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "载入房间";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "载入房间信息异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

    }
}
